package example.step4;

import java.util.ArrayList;
import java.util.List;

public class FieldSignature {
    public static String getClassSignature(Class<?> cl) {
        StringBuilder sbuf = new StringBuilder();
        while (cl.isArray()) {
            sbuf.append('[');
            cl = cl.getComponentType();
        }
        if (cl.isPrimitive()) {
            if (cl == Integer.TYPE) {
                sbuf.append('I');
            } else if (cl == Byte.TYPE) {
                sbuf.append('B');
            } else if (cl == Long.TYPE) {
                sbuf.append('J');
            } else if (cl == Float.TYPE) {
                sbuf.append('F');
            } else if (cl == Double.TYPE) {
                sbuf.append('D');
            } else if (cl == Short.TYPE) {
                sbuf.append('S');
            } else if (cl == Character.TYPE) {
                sbuf.append('C');
            } else if (cl == Boolean.TYPE) {
                sbuf.append('Z');
            } else if (cl == Void.TYPE) {
                sbuf.append('V');
            } else {
                throw new InternalError();
            }
        } else {
            sbuf.append('L' + cl.getName().replace('.', '/') + ';');
        }
        return sbuf.toString();
    }
    public static void main(String args[]){
        // void类型
        System.out.println(FieldSignature.getClassSignature(void.class));
        // 基础类型
        System.out.println(FieldSignature.getClassSignature(int.class));
        // 基础封装类型
        System.out.println(FieldSignature.getClassSignature(Integer.class));
        // 自定义对象类型
        System.out.println(FieldSignature.getClassSignature(FieldSignature.class));
        // String类型
        System.out.println(FieldSignature.getClassSignature(String.class));
        // 数组类型
        int[] arr = new int[2];
        System.out.println(FieldSignature.getClassSignature(arr.getClass()));
        // 对象数组
        FieldSignature[] signs = new FieldSignature[4];
        System.out.println(FieldSignature.getClassSignature(signs.getClass()));
        // 对象集合
        List<FieldSignature> list = new ArrayList<FieldSignature>();
        System.out.println(FieldSignature.getClassSignature(list.getClass()));
    }
}